home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-07-25 | 3.0 KB | 88 lines | [TEXT/PJMM] |
- { © 1993 Marek Hajek. Hajek 's Solutions. Created 6-12-93 }
- {--This example illustrates Integral Computations }
-
- UNIT Auxiliary;
- INTERFACE
- USES
- Sane;
-
- FUNCTION ComputeIntegral (lowerLimit, upperLimit: Extended;
- partitionCount: LongInt;
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended): Extended;
-
- (* You have to define the Integrand function *)
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended;
- (* Computes the head of the equation *)
- FUNCTION ComputeHead (lowerLimit, upperLimit: Extended;
- partitionCount: LongInt): Extended;
- (* Computes first and last of the equation *)
- FUNCTION FirstAndLast (lowerLimit, upperLimit: Extended;
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended): Extended;
-
- IMPLEMENTATION
-
- {--------------------------IntegrandFunction----------------------------}
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended;
- (* This function is the one inside the integral *)
- (*Translate it into pascal and define it here *)
-
- { This functions computes -> √(X * X * X * X +1 }
- BEGIN
- IntegrandFunction := SQRT(XpwrI(partitionCoordinate, 4) + 1);
- END;
-
- {--------------------------ComputeHead----------------------------}
- FUNCTION ComputeHead (lowerLimit, upperLimit: Extended;
- partitionCount: LongInt): Extended;
- (* Computes the first part of the integral equation, the Head *)
- (* Corresponds to (b - a)/(3*n) *)
-
- BEGIN
- ComputeHead := (upperLimit - lowerLimit) / (3 * partitionCount);
- END;
-
- {--------------------------FirstAndLast----------------------------}
- FUNCTION FirstAndLast (lowerLimit, upperLimit: Extended;
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended): Extended;
- (* Computes the second part of the integral, the FIRST/LAST *)
- (* Corresponds to [f(X0) + f(Xn) *)
-
- BEGIN
- FirstAndLast := IntegrandFunction(lowerLimit) + IntegrandFunction(upperLimit);
- END;
-
- {--------------------------ComputeIntegral----------------------------}
- FUNCTION ComputeIntegral (lowerLimit, upperLimit: Extended;
- partitionCount: LongInt;
- FUNCTION IntegrandFunction (partitionCoordinate: Extended): Extended): Extended;
- VAR
- result: Extended;
- head: Extended;
- partitionIncrement: Extended;
- partitionCoordinate: Extended;
- index: LongInt;
-
- BEGIN
- head := ComputeHead(lowerLimit, upperLimit, partitionCount);
- result := FirstAndLast(lowerLimit, upperLimit, IntegrandFunction);
-
- partitionIncrement := (upperLimit - lowerLimit) / partitionCount;
- partitionCoordinate := lowerLimit;
-
- FOR index := 1 TO partitionCount - 1 DO
- BEGIN
- (* Partition coordinate coresponds to X0, X1, X2,.....Xn *)
- partitionCoordinate := partitionCoordinate + partitionIncrement;
-
- (* Odd index means compute 4 * f(x), even index means compute 2 * f(x) *)
- IF Odd(index) THEN
- result := result + 4 * IntegrandFunction(partitionCoordinate)
- ELSE
- result := result + 2 * IntegrandFunction(partitionCoordinate)
-
- END; (* FOR ... *)
-
- ComputeIntegral := head * result;
- END;
-
- END.